package com.qxczh.common.dto;
import com.qxczh.common.enums.StateEnum;
import com.qxczh.common.project.constant.Constant.SysConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * Rest响应数据
 */
@Data
public class Result implements Serializable {

    /**
     * 响应业务状态
     */
    private Integer status;

    /**
     * 返回的数据
     */
    private Object data;

    /**
     * 错误信息
     */
    private String error;

    /**
     * 返回总页数
     */
    private long pageSize;

    /**
     * 本页返回数量
     */
    private Integer total;


    private Result(){}
    public static Result success(){
        Result result = new Result();
        result.setStatus(SysConstant.ResponseCode.SUCCESS);
        result.setError("SUCCESS");
        return result;
    }
    public static Result fail(){
        Result result = new Result();
        result.setStatus(SysConstant.ResponseCode.FAIL);
        result.setError("FAIL");
        return result;
    }

    public static Result partFail(){
        Result result = new Result();
        result.setStatus(SysConstant.ResponseCode.PART_FAIL);
        result.setError("PART_FAIL");
        return result;
    }


    public Result status(Integer status){
        this.setStatus(status);
        return this;
    }
    public Result message(String error){
        this.setError(error);
        return this;
    }

    public Result data(Object value){
        this.data=value;
        return this;
    }

    /**
     * 请求成功
     * @param status
     */
    public Result(Integer status) {
        this.status = status;
    }

    /**
     * 请求成功并携带数据
     * @param status
     * @param data
     */
    public Result(Integer status, Object data) {
        this.status = status;
        this.data = data;
    }

    /**
     * 请求成功被携带参数与页数
     * @param status
     * @param data
     * @param pageSize
     * @param total
     */
    public Result(Integer status, Object data, long pageSize, Integer total) {
        this.status = status;
        this.data = data;
        this.pageSize = pageSize;
        this.total = total;
    }

    /**
     * 请求失败
     * @param status
     * @param error
     */
    public Result(Integer status, String error) {
        this.status = status;
        this.error = error;
    }

    public static Result ok(){
        return new Result(StateEnum.SUCCESS.getState());
    }

    public static Result ok(Object data){
        return new Result(StateEnum.SUCCESS.getState(),data);
    }

    public static Result warn(String warn){return new Result(StateEnum.WARN.getState(),warn);}

    public static Result error(String error){
        return new Result(StateEnum.ERROR.getState(),error);
    }

    public static Result ok(Object data, long pageSize, Integer total){
        return new Result(StateEnum.SUCCESS.getState(),data,pageSize,total);
    }
}
